import { defineStore } from 'pinia'
import { ref } from 'vue'
import { request } from '../utils/request'
import { 
  getPageInfo, 
  getFeatureText as getFeatureTextHelper,
  getStatLabel as getStatLabelHelper,
  getCertificationDescription as getCertificationDescriptionHelper,
  getMultiLangField,
  safeParseJSON
} from './aboutHelpers'

interface AdvantageItem {
  title_en: string
  title_es: string
  title_ja: string
  description_en: string
  description_es: string
  description_ja: string
  image: string
  order: number
}

interface AdvantageSlide {
  image: string
  order: number
}

interface QualityContentBlock {
  title_en: string
  title_es: string
  title_ja: string
  content_en: string
  content_es: string
  content_ja: string
  image: string
  layout: 'left' | 'right'
  order: number
}

interface QualityCertification {
  name: string
  image: string
  description_en: string
  description_es: string
  description_ja: string
  order: number
}

interface AboutMainSlide {
  image: string
  order: number
}

interface QualitySlide {
  image: string
  order: number
}

interface FeatureItem {
  id: string
  text_en: string
  text_es: string
  text_ja: string
  icon?: string
  order: number
}

interface StatItem {
  id: string
  number: string
  label_en: string
  label_es: string
  label_ja: string
  order: number
}

interface AboutSettings {
  // 关于我们主页配置
  about_main_title_en?: string
  about_main_title_es?: string
  about_main_title_ja?: string
  about_main_subtitle_en?: string
  about_main_subtitle_es?: string
  about_main_subtitle_ja?: string
  about_main_description1_en?: string
  about_main_description1_es?: string
  about_main_description1_ja?: string
  about_main_description2_en?: string
  about_main_description2_es?: string
  about_main_description2_ja?: string
  about_main_description3_en?: string
  about_main_description3_es?: string
  about_main_description3_ja?: string
  about_main_slides?: AboutMainSlide[]
  // 新增关于我们页面企业官网样式配置
  about_main_section_title_en?: string
  about_main_section_title_es?: string
  about_main_section_title_ja?: string
  about_main_content_description_en?: string
  about_main_content_description_es?: string
  about_main_content_description_ja?: string
  about_main_features?: FeatureItem[]
  about_main_stats?: StatItem[]
  
  // 企业优势页面配置
  advantage_title_en?: string
  advantage_title_es?: string
  advantage_title_ja?: string
  advantage_subtitle_en?: string
  advantage_subtitle_es?: string
  advantage_subtitle_ja?: string
  advantage_description_en?: string
  advantage_description_es?: string
  advantage_description_ja?: string
  advantage_slides?: AdvantageSlide[]
  advantage_items?: AdvantageItem[]
  
  // 质量保证页面配置
  quality_title_en?: string
  quality_title_es?: string
  quality_title_ja?: string
  quality_subtitle_en?: string
  quality_subtitle_es?: string
  quality_subtitle_ja?: string
  quality_description_en?: string
  quality_description_es?: string
  quality_description_ja?: string
  quality_slides?: QualitySlide[]
  quality_content_blocks?: QualityContentBlock[]
  
  [key: string]: any
}

export const useAboutStore = defineStore('about', () => {
  const settings = ref<AboutSettings>({})
  const loading = ref(false)
  const error = ref<string | null>(null)
  
  // 获取关于我们页面配置
  const fetchSettings = async () => {
    loading.value = true
    error.value = null
    
    try {
      const response = await request.get('/settings/public') as any
      // 响应拦截器已经解包了成功的响应，直接使用 response
      if (response && response.data) {
        // 直接使用返回的配置数据，因为后端已经过滤了相关的配置
        settings.value = response.data || {}
        console.log('用户端获取到的配置数据:', response.data)
      } else {
        error.value = response?.message || '获取配置失败'
      }
    } catch (err: any) {
      error.value = err.message || '网络错误'
      console.error('获取关于我们配置失败:', err)
    } finally {
      loading.value = false
    }
  }
  
  // 根据语言获取主页标题
  const getMainTitle = (lang: string) => {
    return getMultiLangField(settings.value, 'about_main_title', lang, 'About Us')
  }
  
  // 根据语言获取主页副标题
  const getMainSubtitle = (lang: string) => {
    return getMultiLangField(settings.value, 'about_main_subtitle', lang)
  }
  
  // 根据语言获取主页描述
  const getMainDescription = (index: number, lang: string) => {
    return getMultiLangField(settings.value, 'about_main_description', lang)
  }
  
  // 根据语言获取企业优势标题
  const getAdvantageTitle = (lang: string) => {
    return getMultiLangField(settings.value, 'advantage_title', lang, 'Our Advantages')
  }
  
  // 根据语言获取企业优势副标题
  const getAdvantageSubtitle = (lang: string) => {
    return getMultiLangField(settings.value, 'advantage_subtitle', lang)
  }
  
  // 获取企业优势列表
  const getAdvantageItems = () => {
    return settings.value.advantage_items || []
  }
  
  // 根据语言获取优势页面描述
  const getAdvantageDescription = (lang: string) => {
    return getMultiLangField(settings.value, 'advantage_description', lang)
  }
  
  // 获取主页面轮播图
  const getMainSlides = () => {
    return settings.value.about_main_slides || []
  }
  
  // 获取优势页面轮播图
  const getAdvantageSlides = () => {
    return settings.value.advantage_slides || []
  }
  
  // 获取质量页面轮播图
  const getQualitySlides = () => {
    return settings.value.quality_slides || []
  }
  
  // 根据语言获取优势项标题
  const getAdvantageItemTitle = (item: AdvantageItem, lang: string) => {
    return getMultiLangField(item, 'title', lang, item.title_en)
  }
  
  // 根据语言获取优势项描述
  const getAdvantageItemDescription = (item: AdvantageItem, lang: string) => {
    return getMultiLangField(item, 'description', lang, item.description_en)
  }
  
  // 根据语言获取质量保证标题
  const getQualityTitle = (lang: string) => {
    return getMultiLangField(settings.value, 'quality_title', lang, 'Quality Assurance')
  }
  
  // 根据语言获取质量保证副标题
  const getQualitySubtitle = (lang: string) => {
    return getMultiLangField(settings.value, 'quality_subtitle', lang)
  }
  
  // 根据语言获取质量保证描述
  const getQualityDescription = (lang: string) => {
    return getMultiLangField(settings.value, 'quality_description', lang)
  }
  
  // 获取质量内容块列表
  const getQualityContentBlocks = () => {
    return settings.value.quality_content_blocks || []
  }

  

  // =============== 企业官网样式配置方法 ===============
  
  // 根据语言获取关于我们页面区域标题
  const getMainSectionTitle = (lang: string) => {
    return getMultiLangField(settings.value, 'about_main_section_title', lang, '关于我们')
  }

  // 根据语言获取关于我们页面内容描述
  const getMainContentDescription = (lang: string) => {
    return getMultiLangField(settings.value, 'about_main_content_description', lang)
  }

  // 获取关于我们页面特性列表
  const getMainFeatures = () => {
    return settings.value.about_main_features || []
  }

  // 获取关于我们页面统计数据
  const getMainStats = () => {
    return settings.value.about_main_stats || []
  }


  // 根据语言获取质量内容块标题
  const getQualityContentBlockTitle = (block: QualityContentBlock, lang: string) => {
    return getMultiLangField(block, 'title', lang)
  }

  // 根据语言获取质量内容块内容
  const getQualityContentBlockContent = (block: QualityContentBlock, lang: string) => {
    return getMultiLangField(block, 'content', lang)
  }

  // 根据语言获取特性文本
  const getFeatureText = (feature: FeatureItem, lang: string) => {
    return getFeatureTextHelper(feature, lang)
  }

  // 根据语言获取统计标签
  const getStatLabel = (stat: StatItem, lang: string) => {
    return getStatLabelHelper(stat, lang)
  }

  
  return {
    settings,
    loading,
    error,
    fetchSettings,
    getMainTitle,
    getMainSubtitle,
    getMainDescription,
    getMainSlides,
    getAdvantageTitle,
    getAdvantageSubtitle,
    getAdvantageDescription,
    getAdvantageSlides,
    getAdvantageItems,
    getAdvantageItemTitle,
    getAdvantageItemDescription,
    getQualityTitle,
    getQualitySubtitle,
    getQualityDescription,
    getQualitySlides,
    getQualityContentBlocks,
    // 新增企业官网样式方法
    getMainSectionTitle,
    getMainContentDescription,
    getMainFeatures,
    getMainStats,
    getQualityContentBlockTitle,
    getQualityContentBlockContent,
    getFeatureText,
    getStatLabel
  }
})